package dgo;

import java.util.Hashtable;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class SimpleInput {
  private static Hashtable previousKeys = new Hashtable();
  private static Hashtable keys = new Hashtable();

  public static void update(GameCanvas canvas){
  
    // Get Previous Keys
    previousKeys.put("up", new Boolean(isPressed("up")));
    previousKeys.put("right", new Boolean(isPressed("right")));
    previousKeys.put("down", new Boolean(isPressed("down")));
    previousKeys.put("left", new Boolean(isPressed("left")));
    previousKeys.put("ok", new Boolean(isPressed("ok")));
    previousKeys.put("cancel", new Boolean(isPressed("cancel")));

    int keyState = canvas.getKeyStates();
    
    // Get New Keys
    keys.put("up", new Boolean((GameCanvas.UP_PRESSED & keyState)!= 0));
    keys.put("right", new Boolean((GameCanvas.RIGHT_PRESSED & keyState)!= 0));
    keys.put("down", new Boolean((GameCanvas.DOWN_PRESSED & keyState)!= 0));
    keys.put("left", new Boolean((GameCanvas.LEFT_PRESSED & keyState)!= 0));
    keys.put("ok", new Boolean((GameCanvas.FIRE_PRESSED & keyState)!= 0));
    keys.put("cancel", new Boolean((GameCanvas.GAME_C_PRESSED & keyState)!= 0));
  }
  
  public static boolean isPressed(String key){
    Boolean b = (Boolean)keys.get(key);
    if(b == null){
      return false;
    }else{
      return b.booleanValue();
    }
  }

  public static boolean isPressedNewly(String key){
    boolean b1 = ((Boolean)previousKeys.get(key)).booleanValue();
    boolean b2 = ((Boolean)keys.get(key)).booleanValue();
    return !b1 && b2;
  }

  public static boolean isPressedRepeating(String key){
    return ((Boolean)keys.get(key)).booleanValue();
  }
}